#!/usr/bin/env python
#-*- coding:utf-8 -*-
import re
    
default_url_fields=[('host',''),('uri','/'),('params','')]
url_field_names=tuple(map(lambda f:f[0],default_url_fields))
url_regex=re.compile(r'^(?:http(?:s)?://)?(?P<%s>[^\/]+)(?P<%s>[^\?]+)?(?:\?(?P<%s>.*))?$' % url_field_names,re.I)

def get_fields_from_url(url):
    try:
        url_fields={}
        m=url_regex.match(url)
        if not m:
            return None
        for name,value in default_url_fields:
            url_fields[name]=m.group(name) or value
        return url_fields
    except Exception,e:
        return None
    
def get_local_dict():
    from wwpy.db.DBI import DBI,dbc
    local_dict={}
    dbi=DBI(coding='gbk',**dbc.db_base_dict)
    rows=dbi.fetch("select list_name,city1,city2,city3,city4 from display_local")
    dbi.close()
    for row in rows:
        local_dict[row[0]]=row[1:]
    return local_dict

def get_cate_dict():
    from wwpy.db.DBI import DBI,dbc
    cate_dict={}
    dbi=DBI(coding='gbk',**dbc.db_base_dict)
    rows=dbi.fetch("select list_name,cate1,cate2,cate3,cate4,cate5 from display_cate")
    dbi.close()
    for row in rows:
        cate_dict[row[0]]=row[1:]
    return cate_dict

default_local_dict={}
default_cate_dict={}

def get_info_from_url(url,local_dict=None,cate_dict=None):
    """
    define your own local_dict,cate_dict.
    local_dict={list_name:[city1,city2,city3,city4]}
    cate_dict={list_name:[cate1,cate2,cate3,cate4,cate5]}
    """
    global default_local_dict
    global default_cate_dict
    try:
        if local_dict is None and not default_local_dict:
            default_local_dict=get_local_dict()
        if local_dict is None:
            local_dict=default_local_dict
        if cate_dict is None and not default_cate_dict:
            default_cate_dict=get_cate_dict()
        if cate_dict is None:
            cate_dict=default_cate_dict
        url_fields=get_fields_from_url(url)
        if url_fields is None:
            return None
        host,uri,params=url_fields['host'],url_fields['uri'],url_fields['params']
        local=host.split('.')[0]
        paths=[str(path) for path in uri.split('/') if path!='']
        lds,cds=[],[]
        for path in paths:
            if path in local_dict:
                lds=[path]
                lds.extend(local_dict[path])
                continue
            if path in cate_dict:
                cds=[path]
                cds.extend(cate_dict[path])
                continue
        if len(lds)==0:
            lds=[local]
            if local in local_dict:
                lds.extend(local_dict[local])
        for idx,name in enumerate(['local_name','city1','city2','city3','city4']):
            try:
                city=lds[idx]
            except IndexError,e:
                city=''
            url_fields[name]=city
        for idx,name in enumerate(['cate_name','cate1','cate2','cate3','cate4','cate5']):
            try:
                cate=cds[idx]
            except IndexError,e:
                cate=''
            url_fields[name]=cate
        return url_fields
    except Exception,e:
        print e
        return None
    
def get_fields_from_params(params):
    """
    process params,switch params into {key:value}
    """
    if not params:
        return None
    try:
        fields=re.split('\?|&',params)
        param_fields={}
        for field in fields:
            if not field:
                continue
            if field.find('=')==-1:
                continue
            key,value=field.split('=',1)
            if key not in param_fields:
                param_fields[key]=value
        return param_fields
    except Exception,e:
        print e
        return None
    
def demo():
    info=get_info_from_url("http://tj.58.com/hebeiqu/hunqing/")
    for name,field in sorted(info.items()):
        print '=>'.join([str(name),str(field)])+"\n"
    info=get_info_from_url("http://bj.58.com/yewu/?jobfrom=mingqi")
    for name,field in sorted(info.items()):
        print '=>'.join([str(name),str(field)])+"\n"
    print get_fields_from_params('?jobfrom=mingqi')
    
if __name__ == '__main__':
    demo()
